package org.skydiveapp;

import org.skydiveapplibrary.R;
import org.skydiveapplibrary.util.RigReminderManager;

import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TabHost.TabSpec;

public class MainActivity extends TabActivity
{
	// tab tags
	private static final String LOG_TAB = "LOG";
	private static final String STATS_TAB = "STATISTICS";
	private static final String GEAR_TAB = "GEAR";
	private static final String MORE_TAB = "MORE";
	
	@Override 
	public void onCreate(Bundle savedInstanceState)
	{ 
		super.onCreate(savedInstanceState);
		
		if (getIntent().getData() != null)
		{
			android.widget.Toast.makeText(this, getIntent().getData().getPath(), android.widget.Toast.LENGTH_LONG).show();
		}
		
		// show main view
		setContentView(R.layout.main);
		
		// get tab host
		TabHost tabHost = getTabHost();
		tabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider);

		// add log book tab	
		View logTabview = createTabView(tabHost.getContext(), getString(R.string.main_log_tab_label), R.drawable.icon_book);
		TabSpec logTab = tabHost.newTabSpec(LOG_TAB);
		logTab.setIndicator(logTabview);
		logTab.setContent(new Intent(this, LogEntryListActivity.class));
		tabHost.addTab(logTab);
		
		// add stats tab
		View statsTabview = createTabView(tabHost.getContext(), getString(R.string.main_summary_tab_label), R.drawable.icon_summary);
		TabSpec statsTab = tabHost.newTabSpec(STATS_TAB);
		statsTab.setIndicator(statsTabview);
		statsTab.setContent(new Intent(this, SummaryActivity.class));
		tabHost.addTab(statsTab);
		
		// add gear tab
		View gearTabview = createTabView(tabHost.getContext(), getString(R.string.main_gear_tab_label), R.drawable.icon_gear);
		TabSpec gearTab = tabHost.newTabSpec(GEAR_TAB);
		gearTab.setIndicator(gearTabview);
		gearTab.setContent(new Intent(this, GearListActivity.class));
		tabHost.addTab(gearTab);

		// add more tab
		View moreTabview = createTabView(tabHost.getContext(), getString(R.string.main_more_tab_label), android.R.drawable.btn_star_big_off);
		TabSpec moreTab = tabHost.newTabSpec(MORE_TAB);
		moreTab.setIndicator(moreTabview);
		moreTab.setContent(new Intent(this, MoreActivity.class));
		tabHost.addTab(moreTab);
		
		// check rig reminders
		RigReminderManager.getInstance().showRigReminderAlert(this);
	}
	
	@Override
	public void onRestoreInstanceState(Bundle state)
	{
		// do nothing, fixes bug with rotating with an exapandable list view
	}
	
	private static View createTabView(final Context context, final String text, int imageID) {
	    View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null);
	    ((TextView) view.findViewById(R.id.tabsText)).setText(text);
	    ((ImageView) view.findViewById(R.id.tabsImage)).setImageResource(imageID);
	    return view;
	}
}
